
package com.android.matmanual;

import android.app.Activity;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

public class HeadsetLoopTest extends Activity {
	/** Called when the activity is first created. */
	Button btnRecord, btnStop, btnExit;
	boolean isRecording = false;
	static final int frequency = 44100;
	static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
	static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
	int recBufSize,playBufSize;
	AudioRecord audioRecord;
	AudioTrack audioTrack;
	WakeLock mWakeLock;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		PowerManager pm =(PowerManager) getSystemService(POWER_SERVICE); 
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "XYTEST"); 
		mWakeLock.acquire(); 
		setContentView(R.layout.receive_test);
		setTitle(getResources().getString(R.string.headset));
		recBufSize = AudioRecord.getMinBufferSize(frequency,
				channelConfiguration, audioEncoding);

		playBufSize=AudioTrack.getMinBufferSize(frequency,
				channelConfiguration, audioEncoding);
		// -----------------------------------------
		audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
				channelConfiguration, audioEncoding, recBufSize);

		audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
				channelConfiguration, audioEncoding,
				playBufSize, AudioTrack.MODE_STREAM);
		//------------------------------------------



		audioTrack.setStereoVolume(1.0f, 1.0f);
		isRecording = true;
		AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);  
		audioManager.setMicrophoneMute(false);     
		audioManager.setWiredHeadsetOn(true);
		//audioManager.setSpeakerphoneOn(true);
		new RecordPlayThread().start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(mWakeLock!=null)
		{
			mWakeLock.release(); 
			mWakeLock = null;
		}
		android.os.Process.killProcess(android.os.Process.myPid());
	}
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
		 	if(keyCode == KeyEvent.KEYCODE_BACK)
		 	{
		 		isRecording = false;
				HeadsetLoopTest.this.finish();
				finish();
		 	}	 
		 	return true;
	 	}

	class RecordPlayThread extends Thread {
		public void run() {
			try {
				byte[] buffer = new byte[recBufSize];
				audioRecord.startRecording();
				
				audioTrack.play();
				while (isRecording) {
					
					int bufferReadResult = audioRecord.read(buffer, 0,
							recBufSize);

					byte[] tmpBuf = new byte[bufferReadResult];
					System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);
					
					audioTrack.write(tmpBuf, 0, tmpBuf.length);
				}
				audioTrack.stop();
				audioRecord.stop();
			} catch (Throwable t) {
				Toast.makeText(HeadsetLoopTest.this, t.getMessage(), 1000);
			}
		}
	};
}
		 